package com.tul.exercise.base.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 */
public class MyApp extends JFrame{
    private List<JMenu> menus = Arrays.asList(
            new JMenu("File"), new JMenu("Edit"), new JMenu("View")
    );

    private List<String> tabContens = new ArrayList<>();

    private JTabbedPane tabs = new JTabbedPane();

    private ActionListener al = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String tabContent = ((JMenuItem) e.getSource()).getText();
            //tabContens.add(((JMenuItem) e.getSource()).getText());

            tabs.addTab(tabContent, new JButton("Tabbed pane "));
        }
    };

    public MyApp() {
        JMenuBar mb = createMenu();
        addMemuActionListener(mb);
        setJMenuBar(mb);
        setLayout(new FlowLayout());

        add(tabs);
    }

    private void addMemuActionListener(JMenuBar mb) {
        for(int i=0;i<mb.getMenuCount();i++) {
            for(int j=0;j<mb.getMenu(i).getItemCount();j++) {
                mb.getMenu(i).getItem(j).addActionListener(al);
            }
        }
    }

    /**
     * 创建菜单
     * @return JMenuBar
     */
    private JMenuBar createMenu() {
        menus.get(0).add(new JMenuItem("New"), KeyEvent.VK_N);
        menus.get(0).add(new JMenuItem("Open",KeyEvent.VK_O));
        menus.get(0).add(new JMenuItem("Open Recent"));

        menus.get(1).add(new JMenuItem("Cut"));
        menus.get(1).add(new JMenuItem("Copy"));
        menus.get(1).add(new JMenuItem("Paste"));
        menus.get(1).add(new JMenuItem("Delete"));

        menus.get(2).add(new JMenuItem("ToolBar"));
        menus.get(2).add(new JMenuItem("ToolWindows"));
        menus.get(2).add(new JMenuItem("ToolButtons"));
        menus.get(2).add(new JMenuItem("NavigationBar"));

        JMenuBar mb = new JMenuBar();
        for(JMenu jm:menus) {
            mb.add(jm);
        }

        return mb;
    }

    public static void main(String[] args) {
        JFrame frame = new MyApp();
        SwingConsole.run(frame, "测试应用", 800, 500);
    }
}
